package com.youlemall.sellergoods.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.youlemall.mapper.ItemCatMapper;
import com.youlemall.pojo.ItemCat;
import com.youlemall.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**商品分类管理实现类
 * 2 * @Author: kevin
 * 3 * @Date: 2019/7/16 20:04
 * 4
 */

@Service
@Transactional
public class ItemCatServiceImpl implements ItemCatService {
	@Autowired
	private ItemCatMapper itemCatMapper;

	@Override
	public void save(ItemCat itemCat) {
		try {
			itemCatMapper.insertSelective(itemCat);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(ItemCat itemCat) {
		try {
			itemCatMapper.updateByPrimaryKeySelective(itemCat);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void delete(Serializable id) {

	}

	@Override
	public void deleteAll(Serializable[] ids) {
		List<Long> list = new ArrayList<>();
		for (Serializable id : ids) {
			list.add((long)id);
			findLeafNode(id,list);
		}
		Example example = new Example(ItemCat.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("id",list);
		itemCatMapper.deleteByExample(example);
	}

	public void findLeafNode(Serializable id, List<Long> list) {
		List<ItemCat> itemCats = findItemCatByParentId((long)id);
		if (itemCats != null && itemCats.size()>0) {
			for (ItemCat itemCat : itemCats) {
				list.add(itemCat.getId());
				findLeafNode(itemCat.getId(),list);
			}
		}
	}

	@Override
	public ItemCat findOne(Serializable id) {
		return null;
	}

	@Override
	public List<ItemCat> findAll() {
		return null;
	}


	@Override
	public List<ItemCat> findItemCatByParentId(Long parentId) {
		return itemCatMapper.findItemCatByParentId(parentId);
	}



}
